<template>
    <div class="goodsDetail">
        <!-- <h3>商品详情-{{ id }}</h3> -->
        <div class="card" @click="previewImage">
            <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
                <van-swipe-item v-for="(item, index) in thumb" :key="index">
                    <img :src="item.src" />
                </van-swipe-item>
            </van-swipe>
        </div>
    </div>
</template>

<script>
import { fetchGoodsImages, fetchGoodsInfo } from "../api/goodslist.js"
import { ImagePreview } from 'vant';
export default {
    props: ['id'],
    data() {
        return {
            show: false, // 显示sku的弹窗
            thumb: [],
            showAddCartBtn: true, // 是否显示sku中加入购物车的按钮
            goodsInfo: {},
            sku: {
                // 数据结构见下方文档
                tree: [],
                price: "879.00", // 默认价格（单位元）
                stock_num: 18, // 商品总库存
                hide_stock: false, // 是否隐藏剩余库存
            },
            goods: {
                // 数据结构见下方文档
                picture: ""
            }
        }
    },
    methods: {
        
        async _fetchGoodsImages() {
            let { message } = await fetchGoodsImages(this.id)
            this.thumb = message
            // 赋值给sku商品图片
            this.goods.picture = message.length && message[0].src
        },
        // async _fetchGoodsInfo() {
        //     let { message } = await fetchGoodsInfo(this.id)
        //     let reg = /\width=['"]\w+['"]\s+/i;
        //     message.content = message.content.replace(reg, '')
        //     this.goodsInfo = message;
        //     this.sku.price = message.sell_price;
        // },
        previewImage() {
            // 图片预览
            let imgs = this.thumb.map(item => item.src);
            ImagePreview({
                images: imgs,
                startPosition: 1,
            });
        },
        // toCar(showAddCartBtn) {
        //     // 显示sku,并显示sku中加入购物车按钮
        //     this.show = true;
        //     this.showAddCartBtn = showAddCartBtn;
        // },
        // toBuy(showAddCartBtn) {
        //     // 显示sku
        //     this.show = true;
        //     this.showAddCartBtn = showAddCartBtn;
        // },
        
        // skuBuy(skudata) {
        //     let { goodsId, selectedNum } = skudata
        //     let { sell_price } = this.goodsInfo;
        //     let cartItem = { id: goodsId, number: selectedNum, sell_price, selected: true }
        //     this.$store.commit('addGoodsToCar', cartItem)
        //     this.$router.push('/home/shopcar')
        // },
    },
    created() {

        this._fetchGoodsImages()
        this._fetchGoodsInfo()
    }
}
</script>

<style lang="scss" scoped>
@import "../assets/scss/var.scss";

.goodsDetail-container {
    height: 100vh;
    background: $bgColor;
    padding: 4px;

    .card {
        border-radius: 6px;
        margin-bottom: 8px;
        background: #fff;
        padding: 8px;

        .my-swipe {
            .van-swipe-item {
                height: 240px;
                text-align: center;
                img {
                    // width: 100%;
                    height: 100%;
                }
            }
        }

        .price {
            .sell_price {
                color: #fa865d;
                font-size: 20px;
                margin-right: 20px;
            }

            .market_price {
                color: #666;
                font-size: 14px;
            }
        }

        .title {
            color: #333;
            font-weight: 500;
        }

        .content {
            color: #666;

            ::v-deep img {
                width: 100%;
                height: 100%;
            }
        }
    }

}
</style>